#   Shape manipulation Tcl-routines
#   Copyright (C) 2008 Thomas Vosegaard and Zdenek Tosner
#
#   This file is part of the SIMPSON General NMR Simulation Package
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version. 
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#   Tcl routines in addition to the ones implemented in rfshapes.c
#

proc shape2fid {{shape -1} {arg1 ""} {arg2 ""}} {
  if {$shape == -1} {
    # Not the correct number of arguments
    puts stderr "Usage: <desc> shape2fid <shape> <name> ?<duration>? ?(-ap|-apw|-xy)?\n"
    puts stderr "where <desc>     is the SIMPSON file descriptor returned by the command."
    puts stderr "      <shape>    is the shape descriptor."
    puts stderr "      <duration> is the duration of the shape (optional). If not specified"
    puts stderr "                 it defaults to the number of points in the shape."
    puts stderr "      -ap  creates the fid as amplitude/phase in the real/imaginary part."
    puts stderr "      -apw as above but with the phase wrapped to modulo 360 degrees."
    puts stderr "      -xy  creates the fid with x-y components (default)."
    exit
  }
  set type 1
  if {$arg1 == "-ap" || $arg2 == "-ap"} {set type 2}
  if {$arg1 == "-apw" || $arg2 == "-apw"} {set type 3}
  set sw 1
  if {$arg1 != "" && [string is double $arg1]} {
    set sw [expr 1.0/$arg1]
  } elseif {$arg2 != "" && [string is double $arg2]} {
    set sw [expr 1.0/$arg2]
  }
  if { [catch {shape_len $shape} np] } {
      puts stderr "shape2fid error: shape does not exist"
      exit
   }
  set f [fcreate -type fid -sw $sw -np $np -ref 0]
  set i 1
  switch $type {
    1 {
      set d2r [expr 3.1415927/180]
      foreach l [shape2list $shape] {
        fsetindex $f $i [expr cos($d2r*[lindex $l 1])*[lindex $l 0]] \
                        [expr sin($d2r*[lindex $l 1])*[lindex $l 0]]
        incr i
      }
    }
    2 {
      foreach l [shape2list $shape] {
        fsetindex $f $i [lindex $l 0] [lindex $l 1]
        incr i
      }
    }
    3 {
      foreach l [shape2list $shape] {
        set ph [lindex $l 1]
        set ph [expr fmod($ph, 360)]
        if {$ph < 0} {set ph [expr $ph + 360]}
        fsetindex $f $i [lindex $l 0] $ph
        incr i
      }
    }
  }
  return $f
}

proc shape2varian {{shape -1} {name -1} {maxrf -1}} {
  global par
  if {$name == -1} {
    puts stderr "Usage: shape2varian <shape> <name> ?<maxrf>?"
    exit
  }
  if { [catch {shape_len $shape} np] } {
      puts stderr "shape2varian error: shape does not exist"
      exit 1
   }
  if {$maxrf == -1} {
    set maxrf [shape_ampl $shape -max]
  }
  set fp [open $name w]
  puts $fp "# Shape generated by SIMPSON $::simpson_version"
  puts $fp "# From file: $par(name)"
  puts $fp "# Date: [clock format [clock seconds]]"
  puts $fp "# Points in shape: $np"
  foreach l [shape2list $shape] {
    set ph [lindex $l 1]
    set ph [expr fmod($ph, 360)]
    if {$ph < 0} {set ph [expr $ph + 360]}
    puts $fp [format "%10.1f %10.1f\t 1.0" $ph \
          [expr round([lindex $l 0]/double($maxrf)*1023.0)]]
  }
  
}

proc shape2bruker {{shape -1} {name -1} {maxrf -1}} {
  global par
  if {$name == -1} {
    puts stderr "Usage: shape2bruker <shape> <name> ?<maxrf>?"
    exit
  }
  if { [catch {shape_len $shape} np] } {
      puts stderr "shape2bruker error: shape does not exist"
      exit 1
  }
  if {$maxrf == -1} {
    set maxrf [shape_ampl $shape -max]
  }
  
  set fp [open $name w]
  puts $fp "##TITLE= Shape genereted from file: $par(name)"
  puts $fp "##USAGE= Use for OC-optimized transfer/building block"
  puts $fp "##JCAMP-DX= 5.00 \$\$ Bruker JCAMP library"
  puts $fp "##DATA TYPE= Shape Data"
  puts $fp "##ORIGIN= SIMPSON $::simpson_version"
  puts $fp "##DATE= [clock format [clock seconds] -f "%D"]"
  puts $fp "##TIME= [clock format [clock seconds] -f "%T"]"
  puts $fp "##MINX= 0.000000e+00"
  puts $fp "##MAXX= 1.000000e+02"
  puts $fp "##MINY= 0.000000e+00"
  puts $fp "##MAXY= 3.600000e+02"
  puts $fp "##\$SHAPE_EXMODE= None"
  puts $fp "##\$SHAPE_TOTROT= 0.000000e+00"
  puts $fp "##\$SHAPE_BWFAC= 0.000000e+00"
  puts $fp "##\$SHAPE_INTEGFAC= 0.000000e+00"
  puts $fp "##\$SHAPE_MODE= 1"
  puts $fp "##NPOINTS= $np"
  puts $fp "##XYPOINTS= (XY..XY)"
  foreach l [shape2list $shape] {
    set ph [lindex $l 1]
    set ph [expr fmod($ph, 360)]
    if {$ph < 0} {set ph [expr $ph + 360]}
    puts $fp [format "%.6e, %.6e" \
          [expr [lindex $l 0]/double($maxrf)*100.0] $ph]
  }
  puts $fp "##END= "
  close $fp
}
